package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.aos.eap.R;
import com.aos.eap.adapter.CheckListAdapter;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.CheckBean;
import com.aos.eap.bean.PeCheckBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import static com.aos.eap.utils.AnimationUtils.arrowAnimation;

public class PE_CheckActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.ivInfoArrow)
    ImageView ivInfoArrow;
    @BindView(R.id.llInfoTitle)
    LinearLayout llInfoTitle;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvRepairId)
    TextView tvRepairId;
    @BindView(R.id.tvDesc)
    TextView tvDesc;
    @BindView(R.id.tvRepairType)
    TextView tvRepairType;
    @BindView(R.id.tvRepairChild)
    TextView tvRepairChild;
    @BindView(R.id.tvCreateUser)
    TextView tvCreateUser;
    @BindView(R.id.llInfo)
    LinearLayout llInfo;
    @BindView(R.id.rbPass)
    RadioButton rbPass;
    @BindView(R.id.rbNoPass)
    RadioButton rbNoPass;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.ivCheckArrow)
    ImageView ivCheckArrow;
    @BindView(R.id.llCheckTitle)
    LinearLayout llCheckTitle;
    @BindView(R.id.rvInputList)
    RecyclerView rvInputList;
    @BindView(R.id.llCheck)
    LinearLayout llCheck;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tbBuyoff)
    ToggleButton tbBuyoff;
    @BindView(R.id.tvDcType)
    TextView tvDcType;
    @BindView(R.id.ivDcTypeArrow)
    ImageView ivDcTypeArrow;
    @BindView(R.id.flDcType)
    FrameLayout flDcType;
    @BindView(R.id.llDcType)
    LinearLayout llDcType;

    private PeCheckBean peCheckBean;
    private boolean isCheck = false;
    private int isPass = -1;
    private CheckListAdapter checkListAdapter;
    private FreeUI_GeneralPop dcTypePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pe_check;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);

        rvInputList.setLayoutManager(new LinearLayoutManager(PE_CheckActivity.this));
        rvInputList.setItemAnimator(new DefaultItemAnimator());
        rvInputList.addItemDecoration(new DividerItemDecoration(PE_CheckActivity.this, 1));

        checkListAdapter = new CheckListAdapter(null);
        checkListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkListAdapter.isFirstOnly(true);
        rvInputList.setAdapter(checkListAdapter);
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_GetPEOrder", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<PeCheckBean>>(
                new TypeToken<BaseResponseBean<PeCheckBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<PeCheckBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    peCheckBean = response.body().getResponse().getBody();
                    setPageData(peCheckBean);
                } else {
                    peCheckBean = null;
                    setPageData(new PeCheckBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PeCheckBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_GetPEOrder");
                peCheckBean = null;
                setPageData(new PeCheckBean());
            }
        });
    }

    private void setPageData(PeCheckBean peCheckBean) {
        tvLotId.setText(peCheckBean.getLotID());
        tvRepairId.setText(peCheckBean.getOrderID());
        tvDesc.setText(peCheckBean.getFaultDesc());
        tvRepairType.setText(peCheckBean.getRepairType());
        tvCreateUser.setText(peCheckBean.getFROM_USER());
        tvRepairChild.setText(peCheckBean.getRepairSubtype());
        if (peCheckBean.getIsBuyOff().equalsIgnoreCase("Y")) {
            tbBuyoff.setChecked(true);
            tbBuyoff.setEnabled(false);
        } else {
            tbBuyoff.setChecked(false);
            tbBuyoff.setEnabled(true);
        }
        checkListAdapter.setNewData(peCheckBean.getCheckList());
        etRemark.setText(peCheckBean.getRemark());
    }

    @Override
    protected void initData() {
        tbBuyoff.setOnCheckedChangeListener((buttonView, isChecked) -> {
            isCheck = isChecked;
            llDcType.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("PE Check");
        freeUI_titleView.setLeftListener(view -> showBackDialog(PE_CheckActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (peCheckBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
//            if (isPass == -1) {
//                showToast("请选择确认结果", false);
//                return;
//            }
            String dcType = tvDcType.getText().toString().trim();
            if (llDcType.getVisibility() == View.VISIBLE && FreeApi_StringUtils.isEmpty(dcType)) {
                showToast("请选择DcType", false);
                return;
            }
            boolean isOk = true;
            for (CheckBean checkBean : checkListAdapter.getData()) {
                if (checkBean.getItemValue().length() == 0) {
                    isOk = false;
                    break;
                }
            }
            if (!isOk) {
                showToast("请填写所有检查项", false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", etEqpId.getText().toString().trim());
            map.addProperty("PEID", peCheckBean.getOrderID());
            map.addProperty("LotID", peCheckBean.getLotID());
            map.addProperty("IsBuyOff", isCheck ? "Y" : "N");
//            map.addProperty("Result", isPass == 1 ? "NG" : "OK");
            map.addProperty("Remark", etRemark.getText().toString().trim());
            map.addProperty("DcType", dcType);
            map.add("CheckList", new JsonParser().parse(new Gson().toJson(checkListAdapter.getData())).getAsJsonArray());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_PEComfirm", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(PE_CheckActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_PEComfirm");
            }
        });
    }


    @OnClick({R.id.rbPass, R.id.rbNoPass, R.id.llInfoTitle, R.id.llCheckTitle, R.id.flDcType})
    public void onViewClicked(View view) {
        if (MyUtils.isFastClick()){
            showToast("您点击的太快了",false);
            return;
        }
        switch (view.getId()) {
            case R.id.flDcType:
                if (peCheckBean!=null) {
                    dcTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDcType.getWidth(),
                            peCheckBean.getDcTypeList(), ivDcTypeArrow, (s, i) -> {
                                tvDcType.setText(s);
                            });
                    if (dcTypePop.isShowing()) {
                        dcTypePop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivDcTypeArrow);
                        dcTypePop.showAsDropDown(flDcType, 0, 0);
                    }
                }
                break;
            case R.id.rbPass:
                isPass = 0;
                break;
            case R.id.rbNoPass:
                isPass = 1;
                break;
            case R.id.llInfoTitle:
                if (llInfo.getVisibility() == View.VISIBLE) {
                    llInfo.setVisibility(View.GONE);
                    arrowAnimation(ivInfoArrow, 1);
                } else {
                    llInfo.setVisibility(View.VISIBLE);
                    arrowAnimation(ivInfoArrow, 2);
                }
                break;
            case R.id.llCheckTitle:
                if (llCheck.getVisibility() == View.VISIBLE) {
                    llCheck.setVisibility(View.GONE);
                    arrowAnimation(ivCheckArrow, 1);
                } else {
                    llCheck.setVisibility(View.VISIBLE);
                    arrowAnimation(ivCheckArrow, 2);
                }
                break;
        }
    }
}
